<template>
  <el-container class="main-layout">
    <el-header>
      <div class="header-container">
        <div class="logo-container">
          <h2>SpringBoot示例</h2>
        </div>
        <div class="header-content">
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            @select="handleSelect"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b">
            <el-menu-item index="1">仪表盘</el-menu-item>
            <el-menu-item index="2">图书管理</el-menu-item>
            <el-menu-item index="3">Redis管理</el-menu-item>
            <el-menu-item index="4">个人资料</el-menu-item>
          </el-menu>
          <div class="user-info">
            <el-dropdown @command="handleCommand">
              <span class="user-name">
                {{ currentUser }}
                <el-icon class="el-icon--right"><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </el-header>
    <el-main>
      <router-view />
    </el-main>
    <el-footer>
      <div class="footer">
        <p>© {{ new Date().getFullYear() }} SpringBoot示例项目 by Logan.Li</p>
      </div>
    </el-footer>
  </el-container>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const activeIndex = ref('1')

// 获取当前用户名
const currentUser = computed(() => {
  return localStorage.getItem('username') || '用户'
})

// 处理菜单点击事件
const handleSelect = (key) => {
  switch (key) {
    case '1':
      router.push('/dashboard')
      break
    case '2':
      router.push('/books')
      break
    case '3':
      router.push('/redis')
      break
    case '4':
      router.push('/profile')
      break
  }
}

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    handleLogout()
  } else if (command === 'profile') {
    router.push('/profile')
  }
}

// 处理退出登录
const handleLogout = () => {
  const username = localStorage.getItem('username') || '用户'
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  
  // 设置登出消息，供登录页面显示
  sessionStorage.setItem('logoutMessage', `再见，${username}！您已安全退出系统`)
  
  ElMessage.success('退出登录成功')
  router.push('/login')
}

// 监听路由变化，更新活动菜单
watch(() => route.path, (newPath) => {
  if (newPath.includes('/books')) {
    activeIndex.value = '2'
  } else if (newPath.includes('/redis')) {
    activeIndex.value = '3'
  } else if (newPath.includes('/profile')) {
    activeIndex.value = '4'
  } else if (newPath.includes('/dashboard')) {
    activeIndex.value = '1'
  }
}, { immediate: true })
</script>

<style scoped>
/* 主应用布局 */
.main-layout {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.el-header {
  background-color: #545c64;
  color: #fff;
  padding: 0;
  height: 60px !important;
}

.header-container {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.logo-container {
  width: 200px;
}

.logo-container h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 1;
}

.user-info {
  margin-left: 20px;
}

.user-name {
  cursor: pointer;
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 14px;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-name:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.el-main {
  flex: 1;
  padding: 0;
  margin: 0;
  background-color: #f5f7fa;
  overflow-y: auto;
  width: 100%;
  max-width: 100vw;
}

.el-footer {
  background-color: #545c64;
  color: #fff;
  text-align: center;
  padding: 10px 0;
  height: 40px !important;
}

.footer {
  font-size: 14px;
  margin: 0;
}

.footer p {
  margin: 0;
  color: #fff;
}

/* 覆盖Element Plus样式 */
.el-menu.el-menu--horizontal {
  border-bottom: none;
  flex: 1;
  background-color: transparent !important;
}

.el-menu-item {
  font-size: 14px;
  font-weight: 500;
}

.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
}

.el-menu-item.is-active {
  background-color: rgba(255, 208, 75, 0.2) !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    padding: 0 10px;
  }
  
  .logo-container {
    width: 150px;
  }
  
  .logo-container h2 {
    font-size: 16px;
  }
  
  .el-menu-item {
    font-size: 13px;
    padding: 0 10px;
  }
  
  .user-name {
    font-size: 13px;
    padding: 6px 8px;
  }
  
  .el-main {
    padding: 15px;
  }
}

@media (max-width: 480px) {
  .header-container {
    flex-direction: column;
    height: auto;
    padding: 10px;
  }
  
  .header-content {
    width: 100%;
    flex-direction: column;
    gap: 10px;
  }
  
  .el-header {
    height: auto !important;
  }
  
  .user-info {
    margin-left: 0;
  }
  
  .el-main {
    padding: 10px;
  }
  
  .logo-container {
    width: auto;
    text-align: center;
  }
}
</style>
